package com.ysucode.web.servlet;


import com.alibaba.fastjson.JSON;
import com.ysucode.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 获取用户名和id的接口
 */
@WebServlet("/getUserSessionServlet")
public class GetUserSessionServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8"); //POST请求方式 设置字符输入流编码
        HttpSession session = request.getSession(); //获取session
        User user = (User) session.getAttribute("user"); //获得已登录用户的信息

        //做出响应
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(user));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}